package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.CarinfoMapper;
import cn.wolfcode.crm.mapper.GuaranteebillMapper;
import cn.wolfcode.crm.mapper.ProductInfoMapper;
import cn.wolfcode.crm.query.GuaranteebillQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IGuaranteebillService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class GuaranteebillServiceImpl implements IGuaranteebillService{
    @Autowired
    private GuaranteebillMapper guaranteebillMapper;
    @Autowired
    private CarinfoMapper carinfoMapper;
    @Autowired
    private ProductInfoMapper productInfoMapper;


    public int deleteByPrimaryKey(Long id) {
        return guaranteebillMapper.deleteByPrimaryKey(id);
    }

    public int insert(Guaranteebill record) {
        return guaranteebillMapper.insert(record);
    }

    public Guaranteebill selectByPrimaryKey(Long id) {
        return guaranteebillMapper.selectByPrimaryKey(id);
    }

    public List<Guaranteebill> selectAll() {
        return guaranteebillMapper.selectAll();
    }

    public int updateByPrimaryKey(Guaranteebill record) {
        return guaranteebillMapper.updateByPrimaryKey(record);
    }

    //保存账单和商品的关系
    public void saveNumberAndProductInfo(String number,Long pid){
        guaranteebillMapper.saveNumberAndProductInfo(number,pid);

    };
    //保存暂存单
    public void saveBill(Map<String,Object> billMap) {
        System.out.println(billMap);
        Guaranteebill guaranteebill = new Guaranteebill();
        //用户
        LatentClient client = (LatentClient) billMap.get("client");
        //车辆信息
        Carinfo carinfo = (Carinfo) billMap.get("carinfo");
        //车险信息
        ProductInfo productinfo = (ProductInfo) billMap.get("productinfo");
        //年限
        Integer limitYear = (Integer) billMap.get("limitYear");
        //商品id
        Long pid = (Long) billMap.get("pid");
        //carSize
        Long carsizeId = (Long) billMap.get("carsizeId");

        UUID uuid = UUID.randomUUID();
        String number = 2018 + UUID.randomUUID().toString().substring(0,7);
        guaranteebill.setNumber(number);
        guaranteebill.setApplicant(client);
        guaranteebill.setVdate(new Date());
        guaranteebill.setOp((Employee) SecurityUtils.getSubject().getPrincipal());
        guaranteebill.setCarinfo(carinfo);
        guaranteebill.setState(Guaranteebill.STORAGE_STATE);
        guaranteebill.setLimityear(limitYear);
        //保存关系
        guaranteebillMapper.saveNumberAndProductInfo(number,pid);
        //保存车辆信息
        carinfo.setState(1);
        carinfoMapper.insert(carinfo);
        guaranteebillMapper.saveCarsize(carinfo.getId(),carsizeId);

        //保存总价
       guaranteebill.setTotalprice((productInfoMapper.selectByPrimaryKey(pid).getAnnualfee())*limitYear);
        //保存保单
        guaranteebillMapper.insert(guaranteebill);
    }

    //根据状态查询保单
    public List<Guaranteebill> selectByState(GuaranteebillQueryObject qo) {
        return guaranteebillMapper.selectByState(qo);
    }


    public void audit(Long id) {
        guaranteebillMapper.audit(id, Guaranteebill.WAITAUTID_STATE);
    }

    public void checkClient(Long id, String message) {
        guaranteebillMapper.checkClient(id,Guaranteebill.CHECK_STATE,message);
    }

    //查看批改意见
    public String queryAdvice(Long id) {
        return guaranteebillMapper.queryAdvice(id);
    }
    //根据id查询车辆信息
    public Carinfo queryCarInfo(Integer id) {
        return guaranteebillMapper.queryCarInfo(id);
    }

    //核保
    public void auidSuccess(Long id) {
        Employee auditor = (Employee) SecurityUtils.getSubject().getPrincipal();
        Date auditDate = new Date();
        guaranteebillMapper.auidSuccess(id,Guaranteebill.AUTIDSUCCESS_STATE,auditor,auditDate);

    }

    public void rejuest(Long id) {
        guaranteebillMapper.rejuest(id,Guaranteebill.REJUEST_STATE);
    }


    //查询历史记录分页
    public PageResult queryHistory(QueryObject qo){
        //查总数
        int total = guaranteebillMapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        //查每页数据
        List<Guaranteebill> rows = guaranteebillMapper.queryForList(qo);

        return new PageResult(rows,total);
    }
}
